package com.vlada.studzad.util;

import java.util.List;

import org.apache.tapestry5.grid.ColumnSort;
import org.apache.tapestry5.grid.GridDataSource;
import org.apache.tapestry5.grid.SortConstraint;

import com.vlada.studzad.data.MockDataJobOfferSource;
import com.vlada.studzad.model.JobOffer;


public class JobOfferSource implements GridDataSource{

	private MockDataJobOfferSource dataSource;
	private List<JobOffer> selection;
	private int indexFrom;

	public JobOfferSource(MockDataJobOfferSource jobOffers) {
		this.dataSource = jobOffers;
	}

	public int getAvailableRows() {
		return dataSource.getAllJobOffers().size();
	}

	public void prepare(int indexFrom, int indexTo, List<SortConstraint> arg2) {
		System.out.println("Preparing selection.");
		System.out.println("Index from " + indexFrom + " to " + indexTo);
		String propertyName = arg2.get(0).getPropertyModel() == null ? null : arg2.get(0).getPropertyModel().getPropertyName();
		System.out.println("Property name is: " + propertyName);
		boolean ascending = (arg2.get(0).getColumnSort()==ColumnSort.ASCENDING);
		System.out.println("Sorting order ascending: " + ascending);
		//selection = dataSource.getRange(indexFrom, indexTo);
		this.indexFrom = indexFrom;
	}

	public Object getRowValue(int i) {
		System.out.println("Getting value for row " + i);
		return selection.get(i - this.indexFrom);
	}

	public Class getRowType() {
		return JobOffer.class;
	}
}
